package com.cv.platform.service.label;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.cv.framework.common.utils.object.BeanUtils;
import com.cv.framework.mybatis.core.query.QueryWrapperX;
import com.cv.platform.controller.platform.label.vo.record.CvVideoLabelBatchSaveReqVO;
import com.cv.platform.controller.platform.label.vo.ref.CvVideoLabelRefSaveReqVO;
import com.cv.platform.controller.platform.label.vo.record.CvVideoLabelRespVO;
import com.cv.platform.controller.platform.label.vo.record.CvVideoLabelSaveReqVO;
import com.cv.platform.dal.dataobject.label.CvVideoLabelDO;
import com.cv.platform.dal.dataobject.label.CvVideoLabelRefDO;
import com.cv.platform.dal.mapper.label.CvVideoLabelMapper;
import com.cv.platform.dal.mapper.label.CvVideoLabelRefMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 视频标签业务层接口实现类
 *
 * @author Charles_XDXD
 */
@Service
public class CvVideoLabelServiceImpl implements CvVideoLabelService {

    @Resource
    private CvVideoLabelMapper cvVideoLabelMapper;

    @Resource
    private CvVideoLabelRefMapper cvVideoLabelRefMapper;

    @Override
    public Long insertLabel(CvVideoLabelSaveReqVO reqVO) {
        Long videoId = reqVO.getVideoId();
        String label = reqVO.getLabelName();

        if (ObjUtil.isNotEmpty(videoId) && StrUtil.isNotEmpty(label)) {
            QueryWrapperX<CvVideoLabelDO> wrapper = new QueryWrapperX<CvVideoLabelDO>()
                    .eq("label_name", label);

            // 如果标签不存在，则插入标签
            CvVideoLabelDO cvVideoLabelDO = cvVideoLabelMapper.selectOne(wrapper);
            if (ObjUtil.isEmpty(cvVideoLabelDO)) {
                cvVideoLabelDO = new CvVideoLabelDO();
                cvVideoLabelDO.setLabelName(label);
                cvVideoLabelMapper.insert(cvVideoLabelDO);
            }

            CvVideoLabelRefSaveReqVO cvVideoLabelRefSaveReqVO = new CvVideoLabelRefSaveReqVO();
            cvVideoLabelRefSaveReqVO.setLabelId(cvVideoLabelDO.getId());
            cvVideoLabelRefSaveReqVO.setVideoId(videoId);
            insertLabelRef(cvVideoLabelRefSaveReqVO);
            return cvVideoLabelDO.getId();
        }
        return 0L;
    }

    @Override
    public void insertLabelRef(CvVideoLabelRefSaveReqVO reqVO) {
        Long labelId = reqVO.getLabelId();
        Long videoId = reqVO.getVideoId();

        if (ObjUtil.isNotEmpty(labelId) && ObjUtil.isNotEmpty(videoId)) {
            QueryWrapperX<CvVideoLabelRefDO> wrapper = new QueryWrapperX<CvVideoLabelRefDO>()
                    .eq("label_id", labelId)
                    .eq("video_id", videoId);

            // 如果标签关联不存在，则插入标签关联
            CvVideoLabelRefDO cvVideoLabelRefDO = cvVideoLabelRefMapper.selectOne(wrapper);
            if (ObjUtil.isEmpty(cvVideoLabelRefDO)) {
                cvVideoLabelRefDO = new CvVideoLabelRefDO();
                cvVideoLabelRefDO.setLabelId(labelId);
                cvVideoLabelRefDO.setVideoId(videoId);
                cvVideoLabelRefMapper.insert(cvVideoLabelRefDO);
            }
        }
    }

    @Override
    public List<CvVideoLabelRespVO> getAllLabelByVideoId(Long videoId) {
        if (ObjUtil.isNotEmpty(videoId)) {
            QueryWrapperX<CvVideoLabelRefDO> wrapper = new QueryWrapperX<CvVideoLabelRefDO>()
                    .eq("video_id", videoId);

            List<CvVideoLabelRefDO> cvVideoLabelRefDOList = cvVideoLabelRefMapper.selectList(wrapper);
            if (CollUtil.isEmpty(cvVideoLabelRefDOList)) {
                return Collections.emptyList();
            }

            List<CvVideoLabelRespVO> respVOList = new ArrayList<>();
            for (CvVideoLabelRefDO cvVideoLabelRefDO : cvVideoLabelRefDOList) {
                respVOList.add(BeanUtils.toBean(cvVideoLabelMapper.selectById(cvVideoLabelRefDO.getLabelId()), CvVideoLabelRespVO.class));
            }
            return respVOList;
        }
        return Collections.emptyList();
    }

    @Override
    public void batchInsertOrUpdateLabel(CvVideoLabelBatchSaveReqVO reqVOList) {
        if (ObjUtil.isNotEmpty(reqVOList)) {
            List<CvVideoLabelRespVO> videoOriginalLabelList = getAllLabelByVideoId(reqVOList.getVideoId());
            Set<Long> recordSaveLabelIdSet = new HashSet<>();

            for (String label : reqVOList.getLabels()) {
                CvVideoLabelSaveReqVO reqVO = new CvVideoLabelSaveReqVO();
                reqVO.setLabelName(label);
                reqVO.setVideoId(reqVOList.getVideoId());
                recordSaveLabelIdSet.add(insertLabel(reqVO));
            }

            // 清除已移除的标签关联
            for (CvVideoLabelRespVO cvVideoLabelRespVO : videoOriginalLabelList) {
                if (!recordSaveLabelIdSet.contains(cvVideoLabelRespVO.getId())) {
                    QueryWrapperX<CvVideoLabelRefDO> wrapper = new QueryWrapperX<CvVideoLabelRefDO>()
                            .eq("label_id", cvVideoLabelRespVO.getId())
                            .eq("video_id", reqVOList.getVideoId());
                    cvVideoLabelRefMapper.delete(wrapper);
                }
            }
        }
    }

}
